const Monitor = require('../../utils/monitor.js').Monitor
const util = require('../../utils/util.js')
Page(Monitor.hookApp({
  data: {
    normalBtn: 'fenxiao',
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    istrue: true,
    second: 60,
    rate: wx.getSystemInfoSync().windowWidth / 750,
    clicked: false,
    sexesShow: false,
    phone: ''
  },
  bindinput(e) {
    var name = e.currentTarget.dataset.name;
    this.setData({
      [name]: e.detail.value
    })
  },
  login() {
    var that = this
    if (this.data.istrue && this.data.phone && this.data.ma) {
      util.API('spwx/v1.Shop/verificationLogin', {
        tel: that.data.phone,
        verify: that.data.ma
      }).then(res => {
        let {
          code,
          data,
          msg
        } = res
        if (code == 200) {
          that.extensionAgent();
        } else {
          wx.showToast({
            title: msg,
          })
        }
      })
    } else {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none',
      })
    }
  },
  onShow() {
    this.extensionAgent();
  },
  // 处理进度
  promise() {
    let { pfg, setting}=this.data
    let buzuo;
    if (setting.is_phone == 1 && !pfg[0].check_phone) {
      buzuo = 'phone'
    } else if (setting.is_fans == 1 && !pfg[1].check_fans) {
      buzuo = 'fans'
    } else if (setting.is_goods == 1 && !pfg[2].check_goods) {
      buzuo = 'goods';
   
    } else {
      wx.redirectTo({
        url: '../FenXiaoZhongXin/FenXiaoZhongXin',
      })
    }
    this.setData({
      buzuo
    })
  },
  jumpDetails(e){
    wx.navigateTo({
      url: '/pages/shopDetails/shopDetails?goods_id='+e.currentTarget.dataset.id,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  extensionAgent(type) {
    var that = this
    util.API('spwx/v1.Shop/extensionAgent', {}).then(res => {
      let {
        data
      } = res
      //   data.setting.is_phone =0
      // data.setting.is_fans=1
      //   data.setting.is_goods = 1
      // data.pfg,//
      data.setting.pic = `${data.setting.pic ? data.setting.pic : jsynewshop邀请好友1.png}`     
      that.setData({
        pfg: data.pfg,// [{ check_phone: true }, { check_fans: true }, { check_goods: false }],
        resData: data,
        setting: data.setting
      })
      console.log(that.data.setting)
      this.promise()
    })
  },
  buzou() {

  },
  countdown: function() { //倒计时
    var that = this

    if (this.data.phone.length < 11) {
      wx.showToast({
        title: '请填写正确手机号',
        icon: 'none',
      })
      return false
    }
    that.setData({
      clicked: true
    })
    util.API('spwx/v1.Msgal/msg_dx', {
      tel: this.data.phone
    }).then(res => {
      let {
        data,
        code,
        msg
      } = res
      console.log()
      if (code == 200) {
        var time = setInterval(function() {
          that.setData({
            second: that.data.second - 1
          })
          if (that.data.second == 0) {
            clearInterval(time)
          }
        }, 1000)

      } else {

        wx.showToast({
          title: msg,
          icon: 'none',
        })
      }


    })



  },
  reGet() {
    this.setData({
      second:60,
      ma: ""
    })
    this.countdown()
  },
  changeIsTrue() { // 改变点击对号状态
    this.setData({
      istrue: !this.data.istrue
    })
  },
  onShareAppMessage(e) {
    var that = this
    return {
      title: getApp().globalData.app_name,
      imageUrl: e.from == 'button' ? getApp().globalData.shareImage : '',
      path: 'pages/index/index?scene=normal' + `&member_id=${getApp().globalData.member_id}&type=3`,
      success: () => {
        wx.showToast({
          title: '分享成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: () => {
        wx.showToast({
          title: '分享失败',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },
}))